/**
 * @(#) StockBroker.java
 */

package edu.stthomas.edu.seis770.stocksystem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StockBroker implements EventSubscriber {
    private Map<String, Stock> stocks;

    public StockBroker() {
        stocks = new HashMap<String, Stock>();
        // register with event manager for creation events
        List<String> eventsList = new ArrayList<String>();
        eventsList.add(EventManager.STOCK_CREATE_EVENT);
        EventManager.getUniqueInstance().register(this, eventsList);
    }

    public StockStatus getCurrentStockStatus(String stockSymbol) {
        Stock requestStock = stocks.get(stockSymbol);
        return requestStock.getCurrentStockStatus();
    }

    public void setStock(String stockSymbol, Stock stock) {
        stocks.put(stockSymbol, stock);

    }

    public Stock getStock(String stockSymbol) {
        return stocks.get(stockSymbol);
    }

    // This is the method that gets executed when EventManager notifies us of subscribed events
    @Override
    public void update(Object subject, String event) {
    	Stock newStock = (Stock) subject;
    	stocks.put(newStock.getStockSymbol(), newStock);
        System.out.println("A new stock (" + newStock.getStockSymbol() + ") was added to Stock Broker.");
    }
}
